package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager2.widget.ViewPager2;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.VideoGuessAdapter;
import cn.yeyiu.util.VideoUtil;

/**
 * GuessActivity 类似 douyin 滑动视频 实现方式2
 * 基于 viewpager2 实现
 */

public class Guess2Activity extends AppCompatActivity {
    private VideoGuessAdapter adapter;
    private Handler handler;
    LinearLayoutManager layoutManager;
    private ViewPager2 viewPager;

    @Override // androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_guess2);
        getWindow().getDecorView().setSystemUiVisibility(5124);
        viewInit();
        setRecyclerView();
        handlerInit();
        videoInit();
        eventInit();
    }

    public void viewInit() {
        this.viewPager = findViewById(R.id.video_view_vp);
    }

    public void eventInit() {
        this.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if(position+3>adapter.guessVideoList.size()) {
                    VideoUtil.postGuessVideoList(adapter.guessVideoList);
                }
            }
        });
    }

    public void setRecyclerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        this.layoutManager = linearLayoutManager;
        linearLayoutManager.setOrientation(1);
        VideoGuessAdapter videoGuessAdapter = new VideoGuessAdapter(this);
        this.adapter = videoGuessAdapter;

        this.viewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        this.viewPager.setAdapter(adapter);
    }

    public void videoInit() {
        VideoUtil.postGuessVideoList(this.adapter.guessVideoList);
        new Thread(new Runnable() {

            public void run() {
                do {
                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return;
                    }
                } while (Guess2Activity.this.adapter.guessVideoList.size() < 3);
                Guess2Activity.this.handler.sendEmptyMessage(0);
            }
        }).start();
    }

    public void handlerInit() {
        this.handler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
            public boolean handleMessage(Message message) {
                if (message.what != 0) {
                    return true;
                }
                Guess2Activity.this.adapter.notifyDataSetChanged();
                return true;
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}
